{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 14.12 \u8c03\u8bd5\u57fa\u672c\u7684\u7a0b\u5e8f\u5d29\u6e83\u9519\u8bef\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u7684\u7a0b\u5e8f\u5d29\u6e83\u540e\u8be5\u600e\u6837\u53bb\u8c03\u8bd5\u5b83\uff1f"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u7684\u7a0b\u5e8f\u56e0\u4e3a\u67d0\u4e2a\u5f02\u5e38\u800c\u5d29\u6e83\uff0c\u8fd0\u884c python3 -i someprogram.py \u53ef\u6267\u884c\u7b80\u5355\u7684\u8c03\u8bd5\u3002\n-i \u9009\u9879\u53ef\u8ba9\u7a0b\u5e8f\u7ed3\u675f\u540e\u6253\u5f00\u4e00\u4e2a\u4ea4\u4e92\u5f0fshell\u3002\n\u7136\u540e\u4f60\u5c31\u80fd\u67e5\u770b\u73af\u5883\uff0c\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u6709\u4e0b\u9762\u7684\u4ee3\u7801\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# sample.py\n\ndef func(n):\n    return n + 10\n\nfunc('Hello')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd0\u884c python3 -i sample.py \u4f1a\u6709\u7c7b\u4f3c\u5982\u4e0b\u7684\u8f93\u51fa\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "func(10)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u770b\u4e0d\u5230\u4e0a\u9762\u8fd9\u6837\u7684\uff0c\u53ef\u4ee5\u5728\u7a0b\u5e8f\u5d29\u6e83\u540e\u6253\u5f00Python\u7684\u8c03\u8bd5\u5668\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import pdb\npdb.pm()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u7684\u4ee3\u7801\u6240\u5728\u7684\u73af\u5883\u5f88\u96be\u83b7\u53d6\u4ea4\u4e92shell\uff08\u6bd4\u5982\u5728\u67d0\u4e2a\u670d\u52a1\u5668\u4e0a\u9762\uff09\uff0c\n\u901a\u5e38\u53ef\u4ee5\u6355\u83b7\u5f02\u5e38\u540e\u81ea\u5df1\u6253\u5370\u8ddf\u8e2a\u4fe1\u606f\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import traceback\nimport sys\n\ntry:\n    func(arg)\nexcept:\n    print('**** AN ERROR OCCURRED ****')\n    traceback.print_exc(file=sys.stderr)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8981\u662f\u4f60\u7684\u7a0b\u5e8f\u6ca1\u6709\u5d29\u6e83\uff0c\u800c\u53ea\u662f\u4ea7\u751f\u4e86\u4e00\u4e9b\u4f60\u770b\u4e0d\u61c2\u7684\u7ed3\u679c\uff0c\n\u4f60\u5728\u611f\u5174\u8da3\u7684\u5730\u65b9\u63d2\u5165\u4e00\u4e0b print() \u8bed\u53e5\u4e5f\u662f\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002\n\u4e0d\u8fc7\uff0c\u8981\u662f\u4f60\u6253\u7b97\u8fd9\u6837\u505a\uff0c\u6709\u4e00\u4e9b\u5c0f\u6280\u5de7\u53ef\u4ee5\u5e2e\u52a9\u4f60\u3002\n\u9996\u5148\uff0ctraceback.print_stack() \u51fd\u6570\u4f1a\u4f60\u7a0b\u5e8f\u8fd0\u884c\u5230\u90a3\u4e2a\u70b9\u7684\u65f6\u5019\u521b\u5efa\u4e00\u4e2a\u8ddf\u8e2a\u6808\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def sample(n):\n    if n > 0:\n            sample(n-1)\n    else:\n            traceback.print_stack(file=sys.stderr)\nsample(5)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53e6\u5916\uff0c\u4f60\u8fd8\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u4f7f\u7528 pdb.set_trace() \u5728\u4efb\u4f55\u5730\u65b9\u624b\u52a8\u7684\u542f\u52a8\u8c03\u8bd5\u5668\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import pdb\n\ndef func(arg):\n    ...\n    pdb.set_trace()\n    ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u7a0b\u5e8f\u6bd4\u8f83\u5927\u800c\u4f60\u60f3\u8c03\u8bd5\u63a7\u5236\u6d41\u7a0b\u4ee5\u53ca\u51fd\u6570\u53c2\u6570\u7684\u65f6\u5019\u8fd9\u4e2a\u5c31\u6bd4\u8f83\u6709\u7528\u4e86\u3002\n\u4f8b\u5982\uff0c\u4e00\u65e6\u8c03\u8bd5\u5668\u5f00\u59cb\u8fd0\u884c\uff0c\u4f60\u5c31\u80fd\u591f\u4f7f\u7528 print \u6765\u89c2\u6d4b\u53d8\u91cf\u503c\u6216\u6572\u51fb\u67d0\u4e2a\u547d\u4ee4\u6bd4\u5982 w \u6765\u83b7\u53d6\u8ffd\u8e2a\u4fe1\u606f\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0d\u8981\u5c06\u8c03\u8bd5\u5f04\u7684\u8fc7\u4e8e\u590d\u6742\u5316\u3002\u4e00\u4e9b\u7b80\u5355\u7684\u9519\u8bef\u53ea\u9700\u8981\u89c2\u5bdf\u7a0b\u5e8f\u5806\u6808\u4fe1\u606f\u5c31\u80fd\u77e5\u9053\u4e86\uff0c\n\u5b9e\u9645\u7684\u9519\u8bef\u4e00\u822c\u662f\u5806\u6808\u7684\u6700\u540e\u4e00\u884c\u3002\n\u4f60\u5728\u5f00\u53d1\u7684\u65f6\u5019\uff0c\u4e5f\u53ef\u4ee5\u5728\u4f60\u9700\u8981\u8c03\u8bd5\u7684\u5730\u65b9\u63d2\u5165\u4e00\u4e0b print()\n\u51fd\u6570\u6765\u8bca\u65ad\u4fe1\u606f\uff08\u53ea\u9700\u8981\u6700\u540e\u53d1\u5e03\u7684\u65f6\u5019\u5220\u9664\u8fd9\u4e9b\u6253\u5370\u8bed\u53e5\u5373\u53ef\uff09\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8c03\u8bd5\u5668\u7684\u4e00\u4e2a\u5e38\u89c1\u7528\u6cd5\u662f\u89c2\u6d4b\u67d0\u4e2a\u5df2\u7ecf\u5d29\u6e83\u7684\u51fd\u6570\u4e2d\u7684\u53d8\u91cf\u3002\n\u77e5\u9053\u600e\u6837\u5728\u51fd\u6570\u5d29\u6e83\u540e\u8fdb\u5165\u8c03\u8bd5\u5668\u662f\u4e00\u4e2a\u5f88\u6709\u7528\u7684\u6280\u80fd\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u4f60\u60f3\u89e3\u5256\u4e00\u4e2a\u975e\u5e38\u590d\u6742\u7684\u7a0b\u5e8f\uff0c\u5e95\u5c42\u7684\u63a7\u5236\u903b\u8f91\u4f60\u4e0d\u662f\u5f88\u6e05\u695a\u7684\u65f6\u5019\uff0c\n\u63d2\u5165 pdb.set_trace() \u8fd9\u6837\u7684\u8bed\u53e5\u5c31\u5f88\u6709\u7528\u4e86\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5b9e\u9645\u4e0a\uff0c\u7a0b\u5e8f\u4f1a\u4e00\u76f4\u8fd0\u884c\u5230\u78b0\u5230 set_trace() \u8bed\u53e5\u4f4d\u7f6e\uff0c\u7136\u540e\u7acb\u9a6c\u8fdb\u5165\u8c03\u8bd5\u5668\u3002\n\u7136\u540e\u4f60\u5c31\u53ef\u4ee5\u505a\u66f4\u591a\u7684\u4e8b\u4e86\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u4f7f\u7528IDE\u6765\u505aPython\u5f00\u53d1\uff0c\u901a\u5e38IDE\u90fd\u4f1a\u63d0\u4f9b\u81ea\u5df1\u7684\u8c03\u8bd5\u5668\u6765\u66ff\u4ee3pdb\u3002\n\u66f4\u591a\u8fd9\u65b9\u9762\u7684\u4fe1\u606f\u53ef\u4ee5\u53c2\u8003\u4f60\u4f7f\u7528\u7684IDE\u624b\u518c\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}